import java.util.*;
public class Leetcode {

}

//leetcode:1:两数之和
class Solution1 {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> hash = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            if(hash.containsKey(target - nums[i])){
                return new int[]{hash.get(target - nums[i]),i};
            }
            hash.put(nums[i],i);
        }
        return new int[0];
    }
}

//leetcode:面试题 01.02：判定是否互为字符串重排
class Solution2 {
    public boolean CheckPermutation(String s1, String s2) {
        int[] map = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            map[s1.charAt(i) - 'a']++;
        }
        for (int i = 0; i < s2.length(); i++) {
            map[s2.charAt(i) - 'a']--;
        }
        for(int i = 0; i < 26 ;i++){
            if(map[i] != 0) {
                return false;
            }
        }
        return true;
    }
}


//leetcode:217:存在重复元素
class Solution3 {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int i = 0;i<nums.length;i++){
            if(!set.add(nums[i])){
                return true;
            }
        }
        return false;
    }
}

//leetcode:219:存在重复元素II
class Solution4 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length;i++) {
            if(map.containsKey(nums[i]) && i - map.get(nums[i]) <= k){
                return true;
            }
            map.put(nums[i],i);
        }
        return false;
    }
}

//leetcode:49:字母异位词分组
class Solution5 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map = new HashMap<>();
        for(String str : strs) {
            char[] arr = str.toCharArray();
            Arrays.sort(arr);
            String key = new String(arr);
            List<String> list = map.getOrDefault(key,new ArrayList<>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<List<String>>(map.values());
    }
}

